<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120">
                <template slot-scope="scope">
                  {{ (page.page - 1) * page.pagesize + scope.$index + 1 }}
                </template>
              </el-table-column>
              <!-- <el-table-column label="序号" width="120" type="index" /> -->
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPermission(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                  >编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="total,sizes,prev,pager,next"
                :page-size="page.pagesize"
                :page-sizes="[2, 6, 12, 20]"
                :total="total"
                @size-change="SizeChange"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="CompanyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="CompanyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="CompanyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="CompanyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出框 -->
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹出框 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permissionList"
          :props="{label:'name'}"
          default-expand-all=""
          show-checkbox
          node-key="id"
          check-strictly
          :default-checked-keys="selectCheck"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, addRole, getRoleDetail, updateRole, assignPremission } from '@/api/setting'
import { transListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      CompanyInfo: {},
      list: [],
      total: 0,
      page: {
        page: 1,
        pagesize: 2
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名字不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '名字长度在 2-20 之间', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' },
          { min: 5, max: 200, message: '简介长度在 5-20 之间', trigger: 'blur' }
        ]
      },
      showDialog: false,
      showPermDialog: false,
      permissionList: [],
      selectCheck: [],
      roleId: ''
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    title() {
      return this.roleForm.id ? '编辑角色' : '增加角色'
    }
  },
  async created() {
    this.CompanyInfo = await getCompanyInfo(this.companyId)
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.total = total
    },
    async deleteRole(id) {
      await this.$confirm('你确定删除这个角色吗？')
      await deleteRole(id)
      this.currentChange()
      this.$message.success('删除成功')
    },
    currentChange(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    SizeChange(newPageSize) {
      this.page.pagesize = newPageSize
      this.getRoleList()
    },
    addRole() {
      this.showDialog = true
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validateField()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async assignPermission(id) {
      this.roleId = id
      const res = await getPermissionList()
      this.permissionList = transListToTreeData(res, '0')
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    async btnPermOK() {
      try {
        // 以前的多选框组因为是 check-box 有 v-model 直接拿双向绑定的数据即可
        // 现在的属性组件数据的绑定是单项的 v-bind:
        // 树形组件提供了一个方法，专门获取当前选中的值
        const newSelect = this.$refs.permTree.getCheckedKeys()
        await assignPremission({ id: this.roleId, permIds: newSelect })
        this.$message.success('分配权限成功')
        this.showPermDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnPermCancel() {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
